<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class Space extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('bbs_account_model');
        $this->load->model('bbs_user_model');
        $this->load->model('users_model');
        $this->load->model('bbs_detail_model');
        $this->load->model('bbs_article_model');
        $this->uuid = $this->action;
        $this->user_id = decrypt_user_id($this->uuid);
        //获取用户信息
        $this->bbs_users = $this->bbs_user_model->get_users($this->user_id);
        if(!$this->bbs_users){
            $this->assign("error", "对不起，您查找的用户不存在");
            $this->display('deleted/index.html');exit;
        }
        
        $this->bbs_users['is_verified'] = ($this->db->query("select * from tan_bbs_user_verified where user_id = ".$this->user_id." and status = 1 ")->num_rows()) ? 1 : 0;
        
        if($this->bbs_userinfo && $this->bbs_userinfo['user_id'] == $this->user_id) web_redirect("account");
        $this->assign('bbs_users', $this->bbs_users);
        $this->assign('uuid', $this->uuid);
        $this->add_visitor_data();
    }

    function index($uuid) {
        //Ta的文章
        $arts = $this->db->select("a.id,a.title,a.authorid,a.summary,a.thumb,a.cat_id,a.tags_name,a.readnum,a.commentnum,a.status,a.create_time,c.name cat_name")->from("bbs_article a")
                ->join("bbs_article_category c","c.id = a.cat_id","left")
                ->where(array("authorid"=>$this->user_id,"status"=>1))->order_by("create_time desc")->limit(5)->get()->result_array();
        foreach ($arts as $key => $val){
            $arts[$key]['create_time'] = date("Y-m-d H:i:s",$val['create_time']);
            $arts[$key]['detail_url'] = web_url("p/".encrypt_article_id($val['id']));
            $arts[$key]['cat_url'] = web_url("p/cat-".$val['cat_id']);
            $arts[$key]['space_url'] = web_url("u/".encrypt_user_id($val['authorid']));
            $tags = array();
            if($val['tags_name']){
                $tags_arr = explode(",", $val['tags_name']);
                foreach ($tags_arr as $v){
                    $v = explode("_", $v);
                    $tags[$v[1]] = array("tid"=>$v[1],"tname"=>$v[0],"turl"=>  web_url("p/tag-".$v[1])); 
                }
            }
            $arts[$key]['tags'] = $tags;
        }
        
        //Ta的问答
        $wendas = $this->db->select("id,title,answernum,tags_name,create_time,answer_id")->from("bbs_wenda")->where(array("questioner_id"=>$this->user_id,"answer_id"=>0))->order_by("id desc")->limit(5)->get()->result_array();
        foreach ($wendas as $key=>$val){
            //最新回答
            $new_answer = $this->db->select("id,content")->from("bbs_wenda_answer")->where(array("wenda_id"=>$val['id'],"is_show"=>1))->order_by("id desc")->get()->row_array();
            $wendas[$key]['create_time'] = $this->set_time_two($val['create_time']);
            $wendas[$key]['detail_url'] = web_url("wenda/".encrypt_wenda_id($val['id']));
            $wendas[$key]['answer_content'] = $new_answer ? mb_strcut(strip_tags(trim($new_answer['content'])),0,100) : "";
            $wendas[$key]['answer_url'] = $new_answer ?  web_url("wenda/".encrypt_wenda_id($val['id'])."/answer-".encrypt_answer_id($new_answer['id'])) : "";
            $tags = array();
            if($val['tags_name']){
                $tags_arr = explode(",", $val['tags_name']);
                foreach ($tags_arr as $v){
                    $v = explode("_", $v);
                    $tags[$v[1]] = array("tid"=>$v[1],"tname"=>$v[0],"turl"=>  web_url("wenda/tag-".$v[1])); 
                }
            }
            $wendas[$key]['tags'] = $tags;
        }
        //Ta的评论
        $comments = $this->db->select("c.*,a.title,a.commentnum,u1.nickname authorid_nickname,u1.head_img authorid_head_img,u2.nickname reply_nickname")
                ->from("bbs_article_comment c")
                ->join("users_info u1","u1.user_id = c.authorid","left")
                ->join("users_info u2","u2.user_id = c.repay_user_id","left")
                ->join("bbs_article a","a.id = c.article_id","left")
                ->where(array("c.uid"=>$this->user_id,"c.is_show"=>1))->order_by("id desc")->limit(5)->get()->result_array();
        
        foreach ($comments as $key => $val){
            $comments[$key]['create_time'] = date("Y-m-d H:i:s",$val['create_time']);
            $comments[$key]['comment_url'] = web_url("p/".encrypt_article_id($val['article_id'])."/comments/".encrypt_comment_id($val['id']));
            $comments[$key]['reply_url'] = $val['repay_user_id'] ? web_url("u/".encrypt_user_id($val['repay_user_id'])) : "";
            $comments[$key]['author_url'] = web_url("u/".encrypt_user_id($val['authorid']));
        }
        
        $this->left_info();
        $this->assign("arts", $arts);
        $this->assign("action", "index");
        $this->assign("wendas", $wendas);
        $this->assign("comments", $comments);
        $this->display('space/index.html');
    }
    //他的个人中心-他的文章
    function arts(){
        $count = $this->db->select("SUM(commentnum) commentnum,SUM(readnum) readnum")->from("bbs_article")->where(array("authorid"=>$this->user_id,"status"=>1))->order_by("create_time desc")->get()->row_array();
        $this->left_info();
        $this->assign("count", $count);
        $this->assign("action", "arts");
        $this->display('space/arts.html');
    }
    
    //他的提问
    function question(){
        $this->wenda_top();
        $this->assign("action", "question");
        $this->assign("act", "question");
        $this->display("space/wenda.html");
    }
    //他的回答
    function answer(){
        $this->wenda_top();
        $this->assign("action", "question");
        $this->assign("act", "answer");
        $this->display("space/wenda.html");
    }
    //他的关注问题
//    function follow_question(){
//        $this->assign("action", "question");
//         $this->assign("act", "follow_question");
//        $this->display("space/wenda.html");
//    }
    
    private function wenda_top(){
        $bbs_count = $this->db->select("question_count,answer_count,answer_best_count,follow_question_count")->from("bbs_count")->where(array("user_id"=>$this->user_id))->order_by("id desc")->get()->row_array();
        $this->assign("count", $bbs_count);
    }
    
    //他的评论
    function comment(){
        $this->left_info();
        $this->assign("action", "comment");
        $this->display("space/comment.html");
    }
    
    //他的关注
    function follows($uuid) {
        
        $this->assign("action", "follows");
        $this->display('space/friend.html');
    }
    //他的粉丝
    function fans($uuid) {
        $this->assign("action", "fans");
        $this->display('space/friend.html');
    }
    //让人个人中心-左侧公共信息
    private function left_info(){
        $base_info = $this->bbs_users;
        $base_info['signature'] = $base_info['signature'] ? $base_info['signature'] : "这个人好懒，什么也没有写～";
        $base_info['message_url'] = web_url("chats/".$base_info['salt']);
        $is_fan = $this->bbs_userinfo ? ($this->bbs_user_model->get_fan_status(array("user_id"=>$this->user_id,"followuid"=>$this->bbs_userinfo['user_id'],"is_del"=>1)) ? 1 : 0) : 0;
        $base_info['is_fan'] = $is_fan;
        $bbs_count = $this->bbs_user_model->get_bbs_count($this->user_id);
        $base_info['following_count'] = $bbs_count['following_count'];
        $base_info['fans_count'] = $bbs_count['fans_count'];
        $base_info['articles_count'] = $bbs_count['articles_count'];
        $base_info['reply_count'] = $bbs_count['reply_count'];
        $base_info['visitors_count'] = $bbs_count['visitors_count'];
        $today_visitors_count = $this->db->select("SUM(num) num")->from("bbs_visit_list")->where(array("space_uid"=>$this->user_id,"day"=>date("Ymd")))->order_by("create_time")->get()->row_array();
        $base_info['today_visitors_count'] = $today_visitors_count['num'] ? $today_visitors_count['num'] : 0;
        $this->assign("baseinfo", $base_info);
    }
    function info($uuid){
        //检查用户是否存在
        $uuid  = $uuid ? $uuid : 0;
        //获取用户信息
        $bbs_users = $this->bbs_user_model->get_users($uuid);
        if(!$bbs_users){
            web_redirect("/forum.php");
        }
    }
    
    
    //添加访客记录
    private function add_visitor_data(){
        if($this->bbs_userinfo){
            $status = $this->db->select("*")->from("bbs_visit_list")->where(array("user_id"=>$this->bbs_userinfo['user_id'],"space_uid"=>$this->user_id,"day"=>date("Ymd")))->order_by("create_time desc")->get()->row_array();
            if($status){
                $query1 = $this->db->query("update tan_bbs_count set visitors_count = visitors_count + 1 where user_id = ".$this->user_id);
                $query2 = $this->db->query("update tan_bbs_visit_list set num = num + 1 where id = ".$status['id']);
                
            }else{
                $data = array();
                $data['user_id'] = $this->bbs_userinfo['user_id'];
                $data['space_uid'] = $this->user_id;
                $data['create_time'] = time();
                $data['month'] = date("Ym");
                $data['day'] = date("Ymd");
                $data['num'] = 1;
                $this->db->trans_begin();
                $this->db->insert("bbs_visit_list",$data);
                $query1 = $this->db->insert_id();
                $query2 = $this->db->query("update tan_bbs_count set visitors_count = visitors_count + 1 where user_id = ".$this->user_id);
                if($query1 && $query2){
                    $this->db->trans_commit();
                }else{
                    $this->db->trans_rollback();
                }
            }
        }
    }
}
